package Game;
/*
 * The configuration screen.
 * Still needs to be hooked up to infrastructure to be finished.
 * To finish it check the finish() method.
 * 
 * Note it expects a window of 770 by 584 due to the images.
 *
 * @author William Kurkian
 */

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.File;
import java.io.Serializable;

import javax.imageio.ImageIO;
import javax.swing.*;

import GUIInterfaces.OregonPanel;

public class ConfigPanel extends JPanel implements ActionListener,OregonPanel,Serializable {

	/**
	 * Create the panel.
	 */
	JPanel cards = new JPanel();
	JPanel card1 = new JPanel();
		JButton ok1 = new JButton("");
		JTextField name = new JTextField();
	JPanel card2 = new JPanel();
		JButton fast = new JButton();
		JButton medium = new JButton();
		JButton slow = new JButton();
		JButton stopped = new JButton();
	JPanel card3 = new JPanel();
		JTextField[] partyMem = new JTextField[] {
			new JTextField(),
			new JTextField(),
			new JTextField(),
			new JTextField(),
			new JTextField()
		};
		JButton ok2 = new JButton("");
		int player = 1;
	JPanel card4 = new JPanel();
		JButton mechanic = new JButton();
		JButton drugDealer = new JButton();
		JButton moesEmployee = new JButton();
		JButton janitor = new JButton();
		JButton wrestler = new JButton();
	JPanel card5 = new JPanel();
		JButton highRation = new JButton();
		JButton mediumRation = new JButton();
		JButton lowRation = new JButton();
		JButton noneRation = new JButton();
	
	GameDriver gd;
	Caravan caravan;
	
	GUIControl parent;
	
	BufferedImage background;
	Pace pace = Pace.MODERATE;
	CharacterType[] profession = new CharacterType[5];
	Ration ration = Ration.MODERATE;
	int pageindex = 0;
	
	/*
	 * Basic constructor
	 * @param parentParam - a reference to the parent in order for the panel to communicate with it.
	 */
	public ConfigPanel(GUIControl parentParam,GameDriver gd) {
		parent = parentParam;
		this.gd = gd;
		this.setLayout(new GridLayout(1,1));
		
		configureButton(ok1,new Rectangle(279,411,215,85));
		
		configureTextField(name,new Rectangle(146,246,487,100),40);
		
		card1.setOpaque(false);
		card1.setLayout(null);
		card1.add(ok1);
		card1.add(name);
		
		configureButton(fast,new Rectangle(70,266,150,90));
		configureButton(medium,new Rectangle(231,266,150,90));
		configureButton(slow,new Rectangle(393,266,150,90));
		configureButton(stopped,new Rectangle(559,266,150,90));
		
		card2.setOpaque(false);
		card2.setLayout(null);
		card2.add(fast);
		card2.add(medium);
		card2.add(slow);
		card2.add(stopped);
		
		configureTextField(partyMem[1],new Rectangle(238,220,350,45),30);
		configureTextField(partyMem[2],new Rectangle(238,260,350,45),30);
		configureTextField(partyMem[3],new Rectangle(238,302,350,45),30);
		configureTextField(partyMem[4],new Rectangle(238,346,350,45),30);
		configureButton(ok2,new Rectangle(279,408,215,79));
		
		card3.setOpaque(false);
		card3.setLayout(null);
		card3.add(partyMem[1]);
		card3.add(partyMem[2]);
		card3.add(partyMem[3]);
		card3.add(partyMem[4]);
		card3.add(ok2);
		
		configureButton(mechanic,new Rectangle(100,240,150,88));
		configureButton(drugDealer,new Rectangle(315,240,150,88));
		configureButton(moesEmployee,new Rectangle(520,240,150,88));
		configureButton(janitor,new Rectangle(205,386,150,88));
		configureButton(wrestler,new Rectangle(415,386,150,88));
		
		card4.setOpaque(false);
		card4.setLayout(null);
		card4.add(mechanic);
		card4.add(drugDealer);
		card4.add(moesEmployee);
		card4.add(janitor);
		card4.add(wrestler);
		
		configureButton(highRation,new Rectangle(70,266,155,90));
		configureButton(mediumRation,new Rectangle(231,266,155,90));
		configureButton(lowRation,new Rectangle(393,266,155,90));
		configureButton(noneRation,new Rectangle(559,266,155,90));
		
		card5.setOpaque(false);
		card5.setLayout(null);
		card5.add(highRation);
		card5.add(mediumRation);
		card5.add(lowRation);
		card5.add(noneRation);
		
		cards.setOpaque(false);
		cards.setLayout(new CardLayout());
		cards.add(card1,1+"");
		cards.add(card2,2+"");
		cards.add(card3,3+"");
		cards.add(card4,4+"");
		cards.add(card5,5+"");
		this.add(cards);
		
		//initalizes to first page
		nextPanel();
		
	}

	/*
	 * Method which configures the inputted button to the necessary characteristics.
	 * Currently it akes it invisible and sets the size.
	 * @param button
	 * @param rectangle - indicates what its size and position should be.
	 */
	private void configureButton(JButton button, Rectangle rectangle) {
		button.setBounds(rectangle);
		button.setOpaque(false);
		button.setContentAreaFilled(false);
		button.setBorderPainted(false);
		button.addActionListener(this);
		
	}
	
	/*
	 * Method which configures the inputted textfield to the necessary characteristics.
	 * Right it makes it invisible, sets the size, and sets the font.
	 *@param textfield
	 *@param rectangle - used to set the bounds of the text field.
	 *@param fontsize
	 */
	private void configureTextField(JTextField textField, Rectangle rectangle,int fontsize) {
		textField.setBounds(rectangle.x,rectangle.y,rectangle.width,rectangle.height);
		textField.setOpaque(false);
		textField.setFont(new Font(textField.getFont().getName(), textField.getFont().getStyle(),fontsize));
		textField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		
	}
	
	/*
	 * (non-Javadoc)
	 * Draws the background image
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g) {
		g.drawImage(background,0,0,null);
	}

	/*
	 * Moves the panel to the next card for configuration and sets the drawing of the next
	 * background
	 */
	private void nextPanel() {
		if (pageindex != 4 || (pageindex == 4 && player == 5) ) {
			pageindex++;
			player =1;
		} else {
			player++;
			while (partyMem[player-1].getText().equals("")) {
				player++;
				if (player > 5) {
					pageindex++;
					player = 1;
				}
					
			}
				
		}
		try {
			if (pageindex != 4)
				background = ImageIO.read(new File(System.getProperty("user.dir")+File.separator+"images"+File.separator+"config"+pageindex+".jpg"));
			else 
				background = ImageIO.read(new File(System.getProperty("user.dir")+File.separator+"images"+File.separator+"config"+pageindex+""+player+".png"));;
		} catch(Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
		CardLayout cLayout = (CardLayout)cards.getLayout();
		cLayout.show(cards,pageindex+"");
		repaint();
		
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == ok1 ) {
			if (!name.getText().equals("")) {
				partyMem[0].setText(name.getText());
				nextPanel();
			} else
				JOptionPane.showMessageDialog(this, "You must enter a name.");
		} else if (arg0.getSource() == fast) {
			pace = Pace.GRUELING;
			nextPanel();
		} else if (arg0.getSource() == medium) {
			pace = Pace.MODERATE;
			nextPanel();
		} else if (arg0.getSource() == slow) {
			pace = Pace.EASY;
			nextPanel();
		} else if (arg0.getSource() == stopped) {
			pace = Pace.RESTING;
			nextPanel();
		} else if (arg0.getSource() == ok2 ) {
			nextPanel();
		} else if (arg0.getSource() == mechanic) {
			profession[player-1] = CharacterType.MECHANIC;
			nextPanel();
		} else if (arg0.getSource() == janitor) {
			profession[player-1] = CharacterType.JANITOR;
			nextPanel();
		} else if (arg0.getSource() == drugDealer) {
			profession[player-1] = CharacterType.DRUGDEALER;
			nextPanel();
		} else if (arg0.getSource() == wrestler) {
			profession[player-1] = CharacterType.WRESTLER;
			nextPanel();
		} else if (arg0.getSource() == moesEmployee) {
			profession[player-1] = CharacterType.MOESEMPLOYEE;
			nextPanel();
		} else if (arg0.getSource() == highRation) {
			ration = Ration.FILLING;
			finish();
		} else if (arg0.getSource() == mediumRation) {
			ration = Ration.MODERATE;
			finish();
		} else if (arg0.getSource() == lowRation) {
			ration = Ration.SPARSE;
			finish();
		} else if (arg0.getSource() == noneRation){
			ration = Ration.NONE;
			finish();
		}
	}
	/*
	 * When all configuration is finished this sends the information back to the main object
	 */
	private void finish() {
		String[] players = {partyMem[0].getText(),partyMem[1].getText(),partyMem[2].getText(),
							partyMem[3].getText(), partyMem[4].getText()};
		
		Party party[] = Party.values();
		
		for (int i = 0; i < party.length; i++) {
			Party member = party[i];
			if (!players[i].equals("")) {
				member.setName(players[i]);
				member.setProfession(profession[i]);
			}
		}
		
		gd.setPace(pace);
		gd.setRation(ration);
		gd.setCaravan(new Caravan(0));
		JOptionPane.showMessageDialog(this, "Configuration is finished. Time to launch the initial store.");
		parent.done(this);
	}

	public GameDriver getGameDriver() {
		
		return gd;
	}

	@Override
	public void setGameDriver(GameDriver gd) {
		this.gd = gd;
	}

	@Override
	public String getProperty() {
		// TODO Auto-generated method stub
		return null;
	}
}
